﻿using System;
using System.Text;
using System.Linq;
using System.Collections.Generic;
using Sustainalytics.Entities.ProductInvolvement;
using Sustainalytics.Utils;
using Foxit.PDF;
using Foxit.PDF.PageElements;
using Foxit.PDF.Imaging;
using System.Reflection;

namespace Sustainalytics.ProductInvolvement.BL
{
    public class ProductInvolvementPDFReportLogicWithTables : IProductInvolvementPDFReportLogic
    {
        readonly ProductInvolvementSerilog _log;

        private const float LeftMargin = 5;

        private const float PageMaxWidth = 545;
        private const float PageHeight = 770;


        public bool ShowLayoutGrid { get; set; }

        private int _pageCounter;

        private Document _document;

        private readonly List<Page> _pages = new List<Page>();

        private static readonly Font CalibriBoldItalic = Sustainalytics.ReportGeneratorFramework.Utils.GetSystemFont("Calibri Bold Italic");
        private static readonly Font CalibriItalic = Sustainalytics.ReportGeneratorFramework.Utils.GetSystemFont("Calibri Italic");
        private static readonly Font CalibriBold = Sustainalytics.ReportGeneratorFramework.Utils.GetSystemFont("Calibri Bold");
        private static readonly Font CalibriRegular = Sustainalytics.ReportGeneratorFramework.Utils.GetSystemFont("Calibri");
        private static readonly Font CalibriLight = Sustainalytics.ReportGeneratorFramework.Utils.GetSystemFont("Calibri Light");
        private static readonly Font CambriaBold = Sustainalytics.ReportGeneratorFramework.Utils.GetSystemFont("Cambria Bold");

        private static readonly FontFamily CalibriFontFamily = new FontFamily("Calibri", CalibriRegular, CalibriBold, CalibriItalic, CalibriBoldItalic);

        private readonly RgbColor _barBackgroundColor = new RgbColor(189, 190, 192);

        //report images
        private static byte[] _companyLogoHeaderImage;
        private static byte[] _companyLogoFooterImage;

        public static string AssemblyDirectory
        {
            get
            {
                string codeBase = Assembly.GetExecutingAssembly().CodeBase;
                UriBuilder uri = new UriBuilder(codeBase);
                string path = Uri.UnescapeDataString(uri.Path);
                return System.IO.Path.GetDirectoryName(path);
            }
        }

        //------------------------------------------------------------------

        public ProductInvolvementPDFReportLogicWithTables()
        {

        }

        public ProductInvolvementPDFReportLogicWithTables(ProductInvolvementSerilog log)
        {
            _log = log;
        }


        public byte[] Generate_PI_PDF_Report(ProductInvolvementData ca, IEnumerable<ProductInvolvementFramework> piIndicators, List<string> userClaimedIndicators)
        {
            var caDto = ProductInvolvementDataDto.FromCompanyAssessment(ca, piIndicators);

            //create pdf document
            _document = new Document
            {
                Creator = "Sustainalytics",
                Author = "Sustainalytics",
                Title = "Product Involvement Report"
            };

            _pageCounter = 0;

            //////////
            LoadStaticImages();
            AddProducts(caDto, piIndicators, userClaimedIndicators);
            AddSources(caDto, userClaimedIndicators);
            //////////

            _pages.ForEach(p =>
            {
                _document.Pages.Add(p);
                _pageCounter++;
            });

            AddDocumentPagination();

            // Create a memory stream object to save to
            byte[] buffer = _document.Draw();

            return buffer;
        }



        private void AddProducts(ProductInvolvementDataDto ca, IEnumerable<ProductInvolvementFramework> piIndicators, List<string> userClaimedIndicators)
        {
            var page = AddNewPage();
            _pages.Add(page);

            var strBld = new StringBuilder();

            float vSpacer = 10f;
            float x = LeftMargin;
            float y = 0;

            #region REPORT HEADER TABLE 

            y += 18;
            Table tblHeader = new Table(x, y, PageMaxWidth, 190, CalibriBold, 12);
            tblHeader.Border.LineStyle = LineStyle.None;
            tblHeader.CellDefault.Border.LineStyle = LineStyle.None;

            tblHeader.Columns.Add(PageMaxWidth / 2);
            tblHeader.Columns.Add(PageMaxWidth / 2);

            Row tblHeader_row1 = tblHeader.Rows.Add();
            tblHeader_row1.Cells.Add(new Label(ca.CompanyName, 0, 0, 340f, 25f, CalibriBold, 24, TextAlign.Left, RgbColor.Black));
            Cell tblHeader_row1_cell2 = tblHeader_row1.Cells.Add(new Image(ImageData.GetImage(_companyLogoHeaderImage), 0, 0, 0.25f));
            tblHeader_row1_cell2.Align = TextAlign.Right;
            tblHeader_row1_cell2.VAlign = VAlign.Top;

            Row tblHeader_row2 = tblHeader.Rows.Add();

            Cell tblHeader_row2_cell1 = tblHeader_row2.Cells.Add("PRODUCT INVOLVEMENT REPORT", CalibriBold, 12, new RgbColor(188, 190, 192), RgbColor.White, 2);
            tblHeader_row2_cell1.Align = TextAlign.Right;
            tblHeader_row2_cell1.VAlign = VAlign.Center;
            page.Elements.Add(tblHeader);

            y += tblHeader.GetRequiredHeight() + vSpacer;

            #endregion

            page.Elements.Add(new Line(x, y, 550, y, 3, RgbColor.DimGray));

            y += vSpacer;

            #region COMPANY INFO TABLE
            Table tblHeader2 = new Table(x, y, PageMaxWidth, 190, CalibriLight, 10);
            tblHeader2.Border.LineStyle = LineStyle.None;
            tblHeader2.CellDefault.Border.LineStyle = LineStyle.None;

            var tblHeader2_columnCount = 3;

            for (int i = 0; i < tblHeader2_columnCount; i++)
            {
                tblHeader2.Columns.Add(PageMaxWidth / tblHeader2_columnCount);
            }

            //COMPANY INFO TABLE ROW1
            Row tblHeader2_row1 = tblHeader2.Rows.Add();

            Cell tblHeader2_row1_cell1 = tblHeader2_row1.Cells.Add(new FormattedTextArea("<p><font color='#5b6065'>Industry: <b>" + ca.PeerGroupName + "</b></font></p>", 0, 0, 200, 15, CalibriFontFamily, 10, false));
            tblHeader2_row1_cell1.Align = TextAlign.Left;
            Cell tblHeader2_row1_cell2 = tblHeader2_row1.Cells.Add(new FormattedTextArea("<p><font color='#5b6065'>Marketcap: <b>" + (ca.MarketCap.HasValue ? ca.MarketCap.Value.ToString("#,##0") : "") + " mm. USD</b></font></p>", 0, 0, 150, 15, CalibriFontFamily, 10, false));
            tblHeader2_row1_cell2.Align = TextAlign.Left;

            //calculate last edited date
            List<PIIndicatorAssessmentDto> indAss = new List<PIIndicatorAssessmentDto>();
            foreach (ProductInvolvementCompanyIndicatorDto ind in ca.Indicators)
            {
                indAss.AddRange(ind.IndicatorAssessments);
            }
            DateTime lastEdited = indAss.Max(o => o.LastEdited);

            tblHeader2_row1.Cells.Add(new FormattedTextArea("<p><font color='#5b6065'>Updated: <b>" + (lastEdited != null ? lastEdited.ToString("dd MMMM yyyy") : "") + "</b></font></p>", 0, 0, 200, 15, CalibriFontFamily, 10, false));
            tblHeader2_row1_cell2.Align = TextAlign.Left;

            //COMPANY INFO TABLE ROW2
            Row tblHeader2_row2 = tblHeader2.Rows.Add();
            Cell tblHeader2_row2_cell1 = tblHeader2_row2.Cells.Add(new FormattedTextArea("<p><font color='#5b6065'>Domicile: <b>" + ca.Country + "</b></font></p>", 0, 0, 100, 15, CalibriFontFamily, 10, false));
            tblHeader2_row2_cell1.Align = TextAlign.Left;

            Cell tblHeader2_row2_cell2 = tblHeader2_row2.Cells.Add(new FormattedTextArea("<p><font color='#5b6065'>Ticker: <b>" + string.Format("{0}:{1}", ca.Exchange, ca.Ticker) + "</b></font></p>", 0, 0, 150, 15, CalibriFontFamily, 10, false));
            tblHeader2_row2_cell2.Align = TextAlign.Left;

            Cell tblHeader2_row2_cell3 = tblHeader2_row2.Cells.Add(new FormattedTextArea("<p><font color='#5b6065'>Employees: <b>" + (ca.Employees.HasValue ? ca.Employees.Value.ToString("#,##0") : "") + "</b></font></p>", 0, 0, 100, 15, CalibriFontFamily, 10, false));
            tblHeader2_row2_cell3.Align = TextAlign.Left;

            page.Elements.Add(tblHeader2);

            y += tblHeader2.GetRequiredHeight() + vSpacer;

            #endregion

            page.Elements.Add(new Rectangle(x, y, 535, 20, _barBackgroundColor, _barBackgroundColor));
            page.Elements.Add(new FormattedTextArea("<p><font color='FFFFFF'><b>Product Involvement</b></font></p>", x + 5f, y + 3f, 500, 15, CalibriFontFamily, 14, false));

            y += 40;

            var enabledPIIndicatorsSymbols = new List<string>();

            #region ALL INDICATORS TABLE

            Table tblIndicators = new Table(x, y, PageMaxWidth, 300, CalibriLight, 10);
            tblIndicators.Border.LineStyle = LineStyle.None;
            tblIndicators.CellDefault.Border.LineStyle = LineStyle.None;

            var tblIndicators_columnCount = 7;

            for (int i = 0; i < tblIndicators_columnCount; i++)
            {
                tblIndicators.Columns.Add(PageMaxWidth / tblIndicators_columnCount - 1f);
            }

            int step = 0;
            Row currentRow = null;
            foreach (ProductInvolvementFramework ind in piIndicators)
            {
                if (step % tblIndicators_columnCount == 0)
                {
                    currentRow = tblIndicators.Rows.Add();
                }

                var crUserInd = ca.Indicators.SingleOrDefault(o => o.Symbol == ind.Symbol);

                Cell currentCell;

                Table tblInd = new Table(0, 0, 75, 90);
                tblInd.Border.LineStyle = LineStyle.None;
                tblInd.CellDefault.Border.LineStyle = LineStyle.None;

                tblInd.Columns.Add(75);
                Row rowIndImage = tblInd.Rows.Add();
                Row rowIndName = tblInd.Rows.Add();

                Cell rowIndImage_cell1;
                Cell rowIndName_cell1;

                if (crUserInd != null && userClaimedIndicators.Contains(crUserInd.Symbol))
                {
                    rowIndImage_cell1 = rowIndImage.Cells.Add(new Image(GetIndicatorImage(crUserInd.Symbol, true), 0, 0, 0.4f));
                    rowIndImage_cell1.Align = TextAlign.Center;

                    var indicatorNameTextArea = new FormattedTextArea("<p><font color='#000000'><b>" + ind.Name + "</b></font></p>", 0, 0, 75f, 15f, CalibriFontFamily, 7.3f, false);
                    indicatorNameTextArea.Style.Paragraph.Align = TextAlign.Center;
                    indicatorNameTextArea.Height = indicatorNameTextArea.GetRequiredHeight();

                    rowIndName_cell1 = rowIndName.Cells.Add(indicatorNameTextArea);
                    rowIndName_cell1.Align = TextAlign.Center;

                    enabledPIIndicatorsSymbols.Add(crUserInd.Symbol);
                }
                else
                {
                    rowIndImage_cell1 = rowIndImage.Cells.Add(new Image(GetIndicatorImage(ind.Symbol, false), 0, 0, 0.4f));
                    rowIndImage_cell1.Align = TextAlign.Center;

                    var indicatorNameTextArea = new FormattedTextArea("<p><font color='#9ea0a2'><b>" + ind.Name + "</b></font></p>", 0, 0, 75f, 15f, CalibriFontFamily, 7.3f, false);
                    indicatorNameTextArea.Style.Paragraph.Align = TextAlign.Center;
                    indicatorNameTextArea.Height = indicatorNameTextArea.GetRequiredHeight();

                    rowIndName_cell1 = rowIndName.Cells.Add(indicatorNameTextArea);
                    rowIndName_cell1.Align = TextAlign.Center;
                }

                currentCell = currentRow.Cells.Add(tblInd);
                currentCell.Align = TextAlign.Center;
                tblInd.Height = tblInd.GetRequiredHeight();
                step++;

            }

            page.Elements.Add(tblIndicators);
            y += tblIndicators.GetRequiredHeight() + vSpacer;

            #endregion

            #region NO DATA MESSAGE

            if (!enabledPIIndicatorsSymbols.Any())
            {
                y += 80;
                page.Elements.Add(new Line(x, y, 540, y, 1f, RgbColor.Black));
                y += 5;
                strBld.Append("<p>No evidence has been found of significant involvement in any of the above mentioned items.</p>");
                var formTxtArea = new FormattedTextArea(strBld.ToString(), x + 5f, y + 5f, 550, 9, CalibriFontFamily, 9, false);
                page.Elements.Add(formTxtArea);
                strBld.Clear();

                return;
            }

            #endregion

            y += 20;

            #region ENABLED INDICATORS TABLE

            // Create an FormattedTextAreaStyle and set the initial properties for the FormattedTextArea
            var style = new FormattedTextAreaStyle(CalibriFontFamily, 9, false)
            {
                Paragraph = { Align = TextAlign.Justify, Indent = 0, SpacingAfter = 0, AllowOrphanLines = false }
            };

            Table tblEnabledIndicators = new Table(x, y, PageMaxWidth, 200, CalibriRegular, 9);
            tblEnabledIndicators.Border.LineStyle = LineStyle.None;
            tblEnabledIndicators.CellDefault.Border.LineStyle = LineStyle.None;

            //width percentages
            tblEnabledIndicators.Columns.Add(PageMaxWidth * 0.15f);
            tblEnabledIndicators.Columns.Add(PageMaxWidth * 0.10f);
            tblEnabledIndicators.Columns.Add(PageMaxWidth * 0.75f);

            int enabledIndicators_Step = 0;
            Row enabledIndicators_currentRow;

            //get only claimed indicators from Dto
            foreach (var symbol in enabledPIIndicatorsSymbols)
            {

                enabledIndicators_currentRow = tblEnabledIndicators.Rows.Add();

                var userPIIndicator = ca.Indicators.SingleOrDefault(o => o.Symbol == symbol);

                var indicatorNameTextArea = new FormattedTextArea("<p><b>" + userPIIndicator.Name + "</b></p>", 3f, 3f, 75f, 10, CalibriFontFamily, 10, false);
                indicatorNameTextArea.Style.Paragraph.Align = TextAlign.Left;
                indicatorNameTextArea.Height = indicatorNameTextArea.GetRequiredHeight();

                Cell cellIndName = enabledIndicators_currentRow.Cells.Add(indicatorNameTextArea);
                cellIndName.Align = TextAlign.Center;

                Cell cellIndImage = enabledIndicators_currentRow.Cells.Add(new Image(GetIndicatorImage(userPIIndicator.Symbol, true), 0, 0, 0.5f));
                cellIndImage.Align = TextAlign.Center;

                strBld.Clear();
                foreach (PIIndicatorAssessmentDto pia in userPIIndicator.IndicatorAssessments)
                {
                    strBld.Append("<p><b>" + pia.CategorySymbolDescription + "</b></p>");

                    strBld.Append("<p></p>");

                    strBld.Append("<p><b>");

                    if (pia.HasLevelOfInvolvement && !pia.LevelOfInvolvement.Start.NearlyEq(-1.00) && !pia.LevelOfInvolvement.Stop.NearlyEq(-1.00))
                    {
                        strBld.Append("Range: " + pia.LevelOfInvolvement.Text);
                    }
                    else if (pia.HasOwnershipPercent && pia.OwnershipPercent > 0)
                    {
                        strBld.Append("Ownership: " + pia.OwnershipPercent * 100 + "%");
                    }

                    strBld.Append("</b></p>");

                    strBld.Append("<p></p>");

                    strBld.Append("<p>" + pia.StandardAnalysis + "</p>");

                    strBld.Append("<p></p>");
                }

                var formTxtArea = new FormattedTextArea(strBld.ToString(), 5f, 5f, (PageMaxWidth * 0.75f - 20f), 100, style);
                formTxtArea.Height = formTxtArea.GetRequiredHeight();
                enabledIndicators_currentRow.Cells.Add(formTxtArea);

                enabledIndicators_Step++;

            }

            //MANAGE OVERFLOW
            if (y + tblEnabledIndicators.GetRequiredHeight() > PageHeight)
            {
                //add what we can on current page
                tblEnabledIndicators.Height = PageHeight - y;
                page.Elements.Add(tblEnabledIndicators);

                //add the overflow on the next page
                page = AddNewPage();
                _pages.Add(page);
                y = 0;

                Table tblEnabledIndicatorsOverflow = tblEnabledIndicators.GetOverflowRows(x, y);
                tblEnabledIndicatorsOverflow.Height = tblEnabledIndicatorsOverflow.GetRequiredHeight();

                page.Elements.Add(tblEnabledIndicatorsOverflow);
            }
            else
            {
                tblEnabledIndicators.Height = tblEnabledIndicators.GetRequiredHeight();
                page.Elements.Add(tblEnabledIndicators);
            }

            #endregion

        }

        private void AddSources(ProductInvolvementDataDto caDto, List<string> userClaimedIndicators)
        {
            const float titleAreaHeight = 30f;

            var page = AddNewPage();
            this._pages.Add(page);

            float vSpacer = 10f;
            float x = LeftMargin;
            float y = 0;

            page.Elements.Add(new Label("Sources", x + 3f, y + 5f, 500f, 15f, CalibriBold, 12, TextAlign.Left, new RgbColor(95, 96, 98)));
            page.Elements.Add(new Line(x, y + 22f, 545f, y + 22f, 3, RgbColor.DimGray));

            y += 25;

            StringBuilder strBldSourceText = new StringBuilder();
            StringBuilder strBldSourceIndNrs = new StringBuilder();

            List<PIDataSource> allSources = new List<PIDataSource>();

            //get only claimed indicators from Dto
            foreach (var indicator in caDto.Indicators.Where(o => userClaimedIndicators.Contains(o.Symbol)))
            {
                foreach (var iass in indicator.IndicatorAssessments)
                {
                    allSources.AddRange(iass.Sources);
                }
            }

            var sourcesByType = allSources.GroupBy(p => p.Type, (key, g) => new { Type = key, Sources = g.ToList() });


            Table tblSources = new Table(x, y, PageMaxWidth, 200, CalibriRegular, 10);
            tblSources.Border.LineStyle = LineStyle.None;
            tblSources.CellDefault.Border.LineStyle = LineStyle.None;

            //WIDTH PERCENTAGES
            tblSources.Columns.Add(PageMaxWidth * 0.10f);
            tblSources.Columns.Add(PageMaxWidth * 0.90f);

            Row tblSources_currentRow;

            foreach (var group in sourcesByType.Where(p => p.Type != PIDataSourceType.None))
            {
                int i = 1;

                foreach (PIDataSource source in group.Sources)
                {
                    tblSources_currentRow = tblSources.Rows.Add();

                    strBldSourceIndNrs.Append("<p><font color='#5b6065'>" + source.Type.ToString() + "." + i + "</font></p>");
                    strBldSourceText.Append("<p><font color='#5b6065'>" + source.Name + "</font></p>");

                    var formTxtAreaSourceIndNrs = new FormattedTextArea(strBldSourceIndNrs.ToString(), 3f, 3f, 40, 10, CalibriFontFamily, 10, false);
                    formTxtAreaSourceIndNrs.Style.Paragraph.Align = TextAlign.Left;

                    Cell cellSrcType = tblSources_currentRow.Cells.Add(formTxtAreaSourceIndNrs);
                    cellSrcType.Align = TextAlign.Left;

                    var formTxtAreaSourceText = new FormattedTextArea(strBldSourceText.ToString(), 3f, 3f, 450, 10, CalibriFontFamily, 10, false);
                    formTxtAreaSourceText.Height = formTxtAreaSourceText.GetRequiredHeight();
                    formTxtAreaSourceText.Style.Paragraph.Align = TextAlign.Left;

                    Cell cellSrcDesc = tblSources_currentRow.Cells.Add(formTxtAreaSourceText);
                    cellSrcDesc.Align = TextAlign.Left;

                    strBldSourceText.Clear();
                    strBldSourceIndNrs.Clear();

                    i++;
                }
            }

            //MANAGE OVERFLOW
            if (y + tblSources.GetRequiredHeight() > PageHeight)
            {
                //add what we can on current page
                tblSources.Height = PageHeight - y;
                page.Elements.Add(tblSources);

                //add the overflow on the next page
                page = AddNewPage();
                _pages.Add(page);
                y = 0;

                Table tblSourcesOverflow = tblSources.GetOverflowRows(x, y);
                tblSourcesOverflow.Height = tblSourcesOverflow.GetRequiredHeight();

                page.Elements.Add(tblSourcesOverflow);
                y += tblSourcesOverflow.GetRequiredHeight() + vSpacer;
            }
            else
            {
                tblSources.Height = tblSources.GetRequiredHeight();
                page.Elements.Add(tblSources);
                y += tblSources.GetRequiredHeight() + vSpacer;
            }

            //Add Disclaimer
            var copyright1 = $"<p><b>Copyright © {DateTime.UtcNow.Year} Sustainalytics. All rights reserved.</b></p>";

            var copyrightText = new StringBuilder();
            copyrightText.Append(copyright1);
            copyrightText.Append("<p>The ownership and all intellectual property rights to this publication/report and the information contained herein are vested exclusively in Sustainalytics and/or its suppliers. Unless otherwise expressly agreed in writing between you and Sustainalytics, you will not be permitted to use this information otherwise than for internal use, nor will you be permitted to reproduce, disseminate, comingle, create derivative works, furnish in any manner, make available to third parties or publish this publication/report, parts hereof or the information contained herein in any form or in any manner, be it electronically, mechanically, through photocopies, recordings.  The information on which this publication/report is based on reflects the situation as on the date of its elaboration. Such information has – fully or partially – been derived from third parties and is therefore subject to continuous modification. " +
                "THE INFORMATION HEREIN IS PROVIDED SOLELY FOR INFORMATIONAL PURPOSES AND THEREFORE ARE NOT AN OFFER TO BUY OR SELL A SECURITY. NEITHER SUSTAINALYTICS NOR ALL ITS THIRD-PARTY SUPPLIERS PROVIDE INVESTMENT ADVICE (AS DEFINED IN THE APPLICABLE JURISDICTION) OR ANY OTHER FORM OF (FINANCIAL) ADVICE AND NOTHING WITHIN THIS PUBLICATION/REPORT CONSTITUTES SUCH ADVICE. SUSTAINALYTICS OBSERVES THE GREATEST POSSIBLE CARE IN USING INFORMATION, HOWEVER THE INFORMATION IS PROVIDED “AS IS” AND NEITHER SUSTAINALYTICS NOR ITS SUPPLIERS ACCEPT ANY LIABILITY FOR DAMAGE ARISING FROM THE USE OF THIS PUBLICATION/REPORT OR INFORMATION CONTAINED HEREIN IN ANY MANNER WHATSOEVER. MOREOVER, SUSTAINALYTICS AND ALL ITS THIRD-PARTY SUPPLIERS DISCLAIM ANY AND ALL WARRANTIES AND REPRESENTATIONS, EXPRESS OR IMPLIED, INCLUDING ANY WARRANTIES OF MERCHANTIBILITY, COMPLETENESS, ACCURACY OR FITNESS FOR A PARTICULAR PURPOSE.</p>");
            copyrightText.Append("<br>");
            copyrightText.Append("<p>This publication/report may contain proprietary information from third parties (Third Party Data) and here you can find additional terms and conditions imposed by the following Third Party Data providers regarding the use of their data:</p>");
            copyrightText.Append("<br>");
            copyrightText.Append("<a href=\"http://www.sustainalytics.com/legal-disclaimers\"><font color=\"F0AB00\"><b>http://www.sustainalytics.com/legal-disclaimers</b></font></a>");
            copyrightText.Append("<p> </p>");

            var disclaimerSourceText = new FormattedTextArea(copyrightText.ToString(), x + 3f, y + 5f, 500, 10, CalibriFontFamily, 10, false);
            var disclaimerHeight = disclaimerSourceText.GetRequiredHeight();

            if (y + titleAreaHeight + disclaimerHeight > PageHeight)
            {
                //new page
                page = AddNewPage();
                _pages.Add(page);
                y = 0;
            }
            else
            {
                //footer
                y = PageHeight - titleAreaHeight - disclaimerHeight;
            }

            page.Elements.Add(new Label("Disclaimer", x + 3f, y + 5f, 500f, 15f, CambriaBold, 14f));
            page.Elements.Add(new Line(x, y + 22f, 545f, y + 22f, 2f, RgbColor.Black));
            y += titleAreaHeight;

            disclaimerSourceText = new FormattedTextArea(copyrightText.ToString(), x + 3f, y + 5f, 500, disclaimerHeight, CalibriFontFamily, 10, false);
            disclaimerSourceText.Style.Paragraph.Align = TextAlign.Left;
            page.Elements.Add(disclaimerSourceText);
        }

        private void AddDocumentPagination()
        {
            var index = 1;
            foreach (var page in _document.Pages.Cast<Page>().Where(page => page != null))
            {
                if (index > 1)
                {
                    page.Elements.Add(new Line(100, 790, 520, 790, 0.5f, RgbColor.Gray));
                    page.Elements.Add(new Image(ImageData.GetImage(_companyLogoFooterImage), LeftMargin + 85f, 781, 0.24f) { Align = Align.Right, VAlign = VAlign.Top });
                }
                else
                {
                    page.Elements.Add(new Line(LeftMargin, 790, 520, 790, 0.5f, RgbColor.Gray));
                }

                page.Elements.Add(new Label(string.Format("{0} | {1}", index, _document.Pages.Count), 500, 785, 50, 10, CalibriRegular, 10, TextAlign.Right, RgbColor.DimGray));

                index++;
            }
        }

        private Page AddNewPage()
        {
            var page = new Page(PageSize.A4, PageOrientation.Portrait, 20);
            if (ShowLayoutGrid)
                page.Elements.Add(new LayoutGrid { ShowTitle = false });

            return page;
        }

        static void LoadStaticImages()
        {
            _companyLogoHeaderImage = Sustainalytics.ReportGeneratorFramework.Utils.GetJpgImage(AssemblyDirectory + @"\Images\sustainalytics_logo_header.jpg");
            _companyLogoFooterImage = Sustainalytics.ReportGeneratorFramework.Utils.GetJpgImage(AssemblyDirectory + @"\Images\sustainalytics_logo_footer.jpg");
        }

        public ImageData GetIndicatorImage(string indicatorSymbol, bool enabled)
        {
            string strOnOff = enabled ? "on" : "off";
            byte[] imgBytes = Sustainalytics.ReportGeneratorFramework.Utils.GetPngFromSvgImage(string.Format("icn-pi-{0}_{1}.svg", indicatorSymbol, strOnOff));
            return ImageData.GetImage(imgBytes);
        }

    }
}